package org.ifilm.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.ifilm.model.EquipmentOffer;
import org.ifilm.model.EquipmentPosting;
import org.ifilm.model.JobApplication;
import org.ifilm.model.UserJobPosting;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author Mustafa Genç
 *
 */
@Repository
public class NoticeDaoImpl implements NoticeDao {
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public UserJobPosting createJobPosting(UserJobPosting jobPosting) {
		return entityManager.merge(jobPosting);
	}
	
	@Override
	public EquipmentPosting createEquipmentPosting(EquipmentPosting equipmentPosting) {
		return entityManager.merge(equipmentPosting);
	};
	
	@Override
	public JobApplication createJobApplication(JobApplication jobApplication) {
		return entityManager.merge(jobApplication);
	}

	@Override
	public EquipmentOffer createEquipmentOffer(EquipmentOffer equipmentOffer) {
		return entityManager.merge(equipmentOffer);
	}
	
	@Override
	public List<UserJobPosting> listJobPostings() {
		try {
			Query query = entityManager.createQuery(listJobPostingsQuery());

			return (List<UserJobPosting>) query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private String listJobPostingsQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select j from jobposting j");
		return strBuild.toString();
	}

	@Override
	public UserJobPosting getUserJobPosting(Long id) {
		try {
			Query query = entityManager.createQuery(getUserJobPostingQuery());
			query.setParameter("id", id);
			
			return (UserJobPosting)query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private String getUserJobPostingQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select j from jobposting j where j.id=:id");
		return strBuild.toString();
	}

	@Override
	public EquipmentPosting getUserEquipmentPosting(Long id) {
		try {
			Query query = entityManager.createQuery(getUserEquipmentPostingQuery());
			query.setParameter("id", id);
			
			return (EquipmentPosting)query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private String getUserEquipmentPostingQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select j from equipmentposting j where j.id=:id");
		return strBuild.toString();
	}

	@Override
	public List<JobApplication> getJobApplications(Long userId) {
		try {
			Query query = entityManager.createQuery(getJobApplicationsQuery());
			query.setParameter("userId", userId);
			
			return (List<JobApplication>)query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private String getJobApplicationsQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select ja from jobapplication ja where ja.applier.id=:userId");
		return strBuild.toString();
	}
	
	@Override
	public void editJobPosting(UserJobPosting jobPosting){
		entityManager.merge(jobPosting);
	}

	@Override
	public void updateJobApplication(JobApplication jobApplication) {
		entityManager.merge(jobApplication);
	}

	@Override
	public JobApplication getJobApplicationById(Long jobApplicationId) {
		try {
			Query query = entityManager.createQuery(getJobApplicationByIdQuery());
			query.setParameter("id", jobApplicationId);
			
			return (JobApplication)query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private String getJobApplicationByIdQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select ja from jobapplication ja where ja.id=:id");
		return strBuild.toString();
	}

	@Override
	public List<EquipmentOffer> getEquipmentOffers(Long userId) {
		try {
			Query query = entityManager.createQuery(getEquipmentOffersQuery());
			query.setParameter("userId", userId);
			
			return (List<EquipmentOffer>)query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private String getEquipmentOffersQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select eo from equipmentoffer eo where eo.offerer.id=:userId");
		return strBuild.toString();
	}

	@Override
	public void deleteJobPosting(Long postingId) {
		try {
			Query query = entityManager.createQuery(deleteJobPostingQuery());
			query.setParameter("postingId", postingId);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String deleteJobPostingQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("delete from jobposting jp where jp.id=:postingId");
		return strBuild.toString();
	}
}